/**
 * Copyright(c) 2010 Ceno Techonologies Co., Ltd.
 *
 * History:
 *   15-2-5 上午11:21 Created by piaohh
 */
package com.jade.bss.beacon.uuid.impl;

import java.util.Date;
import java.util.List;

import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.sql.SqlUtils;
import com.jade.framework.base.util.PagingList;
import com.jade.bss.beacon.uuid.Uuid;
import com.jade.bss.beacon.uuid.UuidDao;

/**
 *
 * @author <a href="mailto:Administrator@ceno.cn">piaohh</a>
 * @version 1.0 15-2-5 上午11:21
 */
@Named ("jade_beacon_uuidDao")
public class JpaUuidDao implements UuidDao
{
    @PersistenceContext (unitName = "default")
    protected EntityManager entityManager;

    public void add(Uuid uuid)
    {
        entityManager.persist(uuid);
    }

    public void update(String id, int status)
    {
//        String sql="UPDATE Uid set status=:status where id=:id";
//        Query query= entityManager.createQuery(sql);
//        query.setParameter("status",status);
//        query.setParameter("id",id);
//        query.executeUpdate();
        Uuid uuid =this.get(id);
        uuid.setStatus(status);
        uuid.setLastUpdateTime(new Date());
        entityManager.merge(uuid);
    }

    public Uuid get(String id)
    {
        return entityManager.find(Uuid.class,id);
    }

    public List<Uuid> list(Criteria criteria)
    {
        String sql = "SELECT entity FROM Uuid entity";
        return SqlUtils.sqlQueryWithoutCount(sql, entityManager, criteria);
    }

    public PagingList<Uuid> query(Criteria criteria)
    {
        String sql = "SELECT entity FROM Uuid entity";
        return SqlUtils.sqlQuery(sql, entityManager, criteria);
    }
}
